<?php

include_once '../tools/DbTools.php';
//开启SESSION功能
session_start();
$username = $_POST['uesrname'];
$password = $_POST['password'];
$newUrl = '';
$msg = '';
$is_valid = false;
if(!empty($username) && !empty($password)){
    //初始化数据库连接
    DbTools::InitDb();
    //$sql = "select * from bankcard where CardNo='$username' and CardPwd='$password'";
    $sql = "select *,(select RealName from accountinfo where accountinfo.AccountId=bankcard.AccountId) RealName
from bankcard where CardNo='$username' and CardPwd='$password'";
    $result = DbTools::select($sql);
    //关闭连接
    DbTools::close();
    if(!empty($result)){
        $is_valid = true;
        $msg = '登入成功';
        $newUrl = 'userInfo.php';
        $token = md5($username.$password.time());
        setcookie('Token',$token,time()+3600,'/');
        $_SESSION[$token] = [
            'uesrname'=> $username,
            'password' =>$password,
            'CardId'=>$result[0]['CardId'],
            'cardNum'=>$result[0]['CardNo'],
            'realName'=>$result[0]['RealName'],
        ];
    }
}
if(!$is_valid){
    $msg = '用户名或密码错误';
    $newUrl = 'loginUi.php';
}
?>


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
</head>
<body>
<input type="hidden" id="newUrl" value="<?php echo $newUrl;?>">
<input type="hidden" id="msg" value="<?php echo $msg;?>">
</body>
<script>
    onload = function () {
        var newUrl = document.getElementById('newUrl').value;
        var msg = document.getElementById('msg').value;
        alert(msg);
        location.href = newUrl;
    }
</script>

</html>

